#include "stm32f10x.h" // Device header
#include "OLED.h"
#include "Delay.h"
#include "bmp.h"
#include "Key.h"
#include "MyRTC.h"
#include "W25Q64.h"
#include "DHT.h"
#include "MPU6050.h"
uint8_t ID;								//定义用于存放ID号的变量 MPU6050
int16_t AX, AY, AZ, GX, GY, GZ;			//定义用于存放各个数据的变量
// 获取最新按键值
uint8_t keyNum = 0;
uint8_t state = 0;
extern char china[][16];
char ArrayRead1[4][16];
uint8_t flag = 1; // 用于二级菜单的进入
uint8_t flag_sub = 1; //三级菜单
unsigned char humi[10];
int main(void)
{
MPU6050_Init();
	OLED_Init();
	Key_Init();
	MyRTC_Init();
	W25Q64_Init();

	while (1)
	{

		MyRTC_GetTime();
		keyNum = Key_GetNum();

		switch (keyNum)
		{
		case 1:
	 OLED_Clear();
			while (flag == 1)
			{
			OLED_ShowString(1, 1, "readData?");
			OLED_ShowString(4, 1, "Yes");
			OLED_ShowString(4, 8, "No");
				keyNum = Key_GetNum();
				if (keyNum == 2)
				{
					OLED_Clear();
					w25SectorErase(0x000000);
					w225PageProgram2(0x000000, china, 4);
					w25read2(0x000000, ArrayRead1, 4);
					OLED_ShowString(1, 1, "Admin:");
					OLED_ShowCHinese(3, 1, 0, ArrayRead1);
					OLED_ShowCHinese(3, 20, 1, ArrayRead1);
					OLED_ShowString(4, 1, "[");
					OLED_ShowCHinese(6, 8, 2, china);
					OLED_ShowCHinese(6, 28, 3, china);
					OLED_ShowString(4, 40, "]");
					while (flag_sub ==1)
					{
						if(Key_GetNum() == 2)
						{
							OLED_Clear();
							flag_sub = 0;
							flag = 0;
						}
					}
					
				}
				if (Key_GetNum() == 3)
				{
					OLED_Clear();
					flag = 4;
				}
			}
//按键二的功能
while (flag==2)
{
	Delay_ms(100);
		OLED_ShowCHinese(1, 1, 4, china);
	OLED_ShowCHinese(1, 21, 5, china);
	 ReadDht11Data(humi);
	
	OLED_ShowNum(1, 8, humi[0], 3);
	OLED_ShowString(1,11, ".");
	OLED_ShowNum(1, 12, humi[1], 3);
	
	 OLED_ShowString(3, 1, "hot:");
	 	OLED_ShowNum(3, 8, humi[2], 3);
	OLED_ShowString(3,11, ".");
	OLED_ShowNum(3, 12, humi[3], 3);


	  //识别按键并且进行返回
	OLED_ShowString(4, 1, "[");
	OLED_ShowCHinese(6,15,2,china);
	OLED_ShowCHinese(6,35,3,china);
	OLED_ShowString(4, 30, "]");
	int sec_key = Key_GetNum();
	if(sec_key == 2)
	{
	flag = 4;//无关数值，主要是推出循环
	}

}
while(flag ==3)
{
/**
  * 函    数：MPU6050获取数据
  * 参    数：AccX AccY AccZ 加速度计X、Y、Z轴的数据，使用输出参数的形式返回，范围：-32768~32767
  * 参    数：GyroX GyroY GyroZ 陀螺仪X、Y、Z轴的数据，使用输出参数的形式返回，范围：-32768~32767
  * 返 回 值：无
  */
		MPU6050_GetData(&AX, &AY, &AZ, &GX, &GY, &GZ);		//获取MPU6050的数据
		OLED_ShowSignedNum(1, 1, AX, 5);					//OLED显示数据
		OLED_ShowSignedNum(2, 1, AY, 5);
		OLED_ShowSignedNum(3, 1, AZ, 5);
		OLED_ShowSignedNum(1, 8, GX, 5);
		OLED_ShowSignedNum(2, 8, GY, 5);
		OLED_ShowSignedNum(3, 8, GZ, 5);
	
	  //识别按键并且进行返回
	OLED_ShowString(4, 1, "[");
	OLED_ShowCHinese(6,15,2,china);
	OLED_ShowCHinese(6,35,3,china);
	OLED_ShowString(4, 20, "]");
	int sec_key = Key_GetNum();
	if(sec_key == 2)
	{
	flag = 4;//无关数值，主要是推出循环
	}

	
}

			break;
		case 2:
			state--;
			break;
		case 3:
			state++;
			break;
		default:
			break;
		}

		
		OLED_ShowNum(8, 1, MyRTC_Time[3], 2); // 时
		OLED_ShowString(8, 3, ":");
		OLED_ShowNum(8, 4, MyRTC_Time[4], 2); // 分
		OLED_ShowString(8, 6, ":");
		OLED_ShowNum(8, 7, MyRTC_Time[5], 2); // 秒

		switch (state)
		{
		case 0:
			OLED_DrawBMP(0, 0, 111, 42, BMP2);
			flag = 1;
			flag_sub = 1;
			break;
		case 1:
			OLED_DrawBMP(0, 0, 111, 42, BMP3);
			flag = 2;
			flag_sub = 2;
			break;
		case 2:
			OLED_DrawBMP(0, 0, 111, 42, BMP4);
			flag = 3;
			flag_sub = 3;

			break;
		default:
			break;
		}
	}
}
